package net.bat.service.cb;


import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.bat.db.BaseDAOImpl;
import net.bat.pt.BObj;
import net.bat.util.Cfg;
import net.cs.um.UMService;
import dao.hb.BxDetailrecord;
import dao.hb.BxSimplerecord;
import dao.hb.UUser;

public class CBBxDetailrecord implements IHandleHook {

	@Override
	public void beforeHandle(char method, HashMap<String, Object> psMap,
			HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		UUser usr = UMService.getUserBySession(request);
		int uid = usr.getId();
		BaseDAOImpl bdi = (BaseDAOImpl) Cfg.getBean("BaseDAOImpl");
		BObj[] objs = (BObj[])psMap.get("objs");
		for(int i=0; i<objs.length; i++){
			BObj cur = objs[i];
			HashMap<String,Object>  its = cur.getItems();
		
			if(cur.getAction()==BObj.ACTION_ADD){
					if(its.get("payOne")==null||its.get("payOne").equals("")){
	              	  its.put("payOne",0);
	                }		
	                if(its.get("payTwo")==null||its.get("payTwo").equals("")){
	              	  its.put("payTwo",0);
	                }
	                if(its.get("payAll")==null||its.get("payAll").equals("")){
	              	  its.put("payAll",0);
	                }
	                if(its.get("topMoney")==null||its.get("topMoney").equals("")){
	              	  its.put("topMoney",0);
	              	}
                  if(its.get("reason")==null){
                	  its.put("reason","");
                  }
                  if(its.get("uid")==null){
                  String sql="from BxSimplerecord where (status='未提交' or status='已提交') and uid="+uid;
                  List l=bdi.find(sql);
                  if(l.size()>0){
                	      BxSimplerecord bs=(BxSimplerecord)l.get(0);
                	               its.put("bxsrPid",bs.getId());
                  }else{
                	  //先创建一张主表
                	  BxSimplerecord bc=new BxSimplerecord();
                	      bc.setUid(uid);
                	      bc.setUname(usr.getName());
                	      bc.setRecorder(usr.getName());
                	      bc.setStatus("未提交");
                	      bc.setDtCreate(new Date());
                          bdi.save(bc);
                      String ql="from BxSimplerecord where status='未提交' and uid="+uid;
                      BxSimplerecord b=(BxSimplerecord)bdi.find(ql).get(0);
                          its.put("bxsrPid",b.getId());
                  }
                          its.put("uid",uid);
                          }
                       /**   
                  if(its.get("uid")==null){
                	  String hql="from BxSimplerecord where status='未提交' and  uid="+uid;
                	  BxSimplerecord bx=(BxSimplerecord)bdi.find(hql).get(0);
	                	  its.put("uid", uid);
	                	  its.put("bxsrPid", bx.getId());
                  }
                  **/
			}
			//用户修改数据值为null后依然设置默认值   add by fucj 2014-05-16
			if(cur.getAction()==BObj.ACTION_UPDATE){
				if(its.containsKey("payOne")&&(its.get("payOne")==null||its.get("payOne").equals(""))){
              	  its.put("payOne",0);
                }		
                if(its.containsKey("payTwo")&&(its.get("payTwo")==null||its.get("payTwo").equals(""))){
              	  its.put("payTwo",0);
                }
                if(its.containsKey("payAll")&&(its.get("payAll")==null||its.get("payAll").equals(""))){
              	  its.put("payAll",0);
                }
                if(its.containsKey("topMoney")&&(its.get("topMoney")==null||its.get("topMoney").equals(""))){
              	  its.put("topMoney",0);
              	}
              if(its.get("reason")==null){
            	  its.put("reason","");
              }

		}
		}
	}

	@Override
	public void afterHandle(char method, HashMap<String, Object> psMap,
			HttpServletRequest request, Object result) throws Exception {
		
		BaseDAOImpl bdi = (BaseDAOImpl) Cfg.getBean("BaseDAOImpl");

		Object[][] objs = (Object[][])result;
		BxDetailrecord cur =(BxDetailrecord) objs[0][0];
		int  id=cur.getBxsrPid();
	
		String hql="from BxSimplerecord where id="+id;
		String dql="from BxDetailrecord where bxsrPid="+id;
		
		List li=bdi.find(dql);
		
		BxSimplerecord bs= (BxSimplerecord)bdi.find(hql).get(0);
		       if(bs.getReceiptsNum()==null){
		    	   bs.setReceiptsNum(0);
		       }
	         bs.setReceiptsNum(li.size());
	         
	      bdi.update(bs);
	      
	      //详细表删除，主表一起删
	      
	      BObj[] bobj = (BObj[])psMap.get("objs");
			for(int i=0; i<objs.length; i++){
				BObj c = bobj[i];
				if(c.getAction()==BObj.ACTION_REMOVE){
					if(li.size()==0){
				    	  bs.setStatus("未提交");
				      }
				}
			}
	      
	}

}
